<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    /**
     *
     * @param int $error_code
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     */
    public function witejson($error_code=0,$data=[],$msg='',$code=200)
    {
        return response()->json([
           'error_code'=>$error_code,
           'data'=>$data,
           'msg'=>$msg
        ],$code);
    }


    /**
     * 验证信息
     * @param $params 验证内容
     * @param $rules 验证规则
     */
    public function validateParams($params,$rules)
    {
        $validate = \Validator::make($params,$rules);

        if($validate->fails()){
            $error = $validate->errors();
            //判断是否是数组
            if(!is_array($error)){
                $error = $error->toArray();
            }
            $errorArr=[];
            foreach ($error as $key=>$val){
                $errorArr[] = $key . ':' . $val[0];
            }
            exit(json_encode($errorArr,256));
        }
        return $params;
    }
}

